<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :model="state.param" :inline="true">
					<el-form-item label="分类类型:">
						<el-select v-model="state.param.type"  placeholder="请选择分类类型" @change="getCategoryList">
							<el-option :key = 0 label = "商城商品分类" :value = 0 />
							<el-option :key = 1 label = "门店商品分类" :value = 1 />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="addCategory('add',{})">
							<el-icon>
								<ele-Plus />
							</el-icon> 新 增
						</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex;flex-direction: column;flex: 1;height: inherit;">
				<section style="display: flex;flex-direction: column; flex: 1;height: inherit;">
					<el-table :data="state.tableData.data" ref="goodTableRef" v-loading="state.tableData.loading">
						<el-table-column label="ID" width="80">
							<template #default="scope">
								<span>{{ scope.row.data.categoryId }}</span>
							</template>
						</el-table-column>
						<el-table-column label="分类名称" >
							<template #default="scope">
								<span>{{ scope.row.data.categoryName }}</span>
							</template>
						</el-table-column>
						<el-table-column label="分类图标" align="center">
							<template #default="scope">
								<div style="display: flex;justify-content:center;align-items: center;">
									<el-image class="ml15" style="width:32px;height: 32px;" :src="scope.row.data.categoryUrl"
										fit="cover" />
								</div>
							</template>
						</el-table-column>
						<el-table-column label="来源" >
							<template #default="scope">
								<span>{{ scope.row.data.sourceName }}</span>
							</template>
						</el-table-column>
						<el-table-column label="创建人" >
							<template #default="scope">
								<span>{{ scope.row.data.createdBy }}</span>
							</template>
						</el-table-column>
						<el-table-column label="创建时间" >
							<template #default="scope">
								<span>{{ scope.row.data.createdTime }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作" >
							<template #default="scope">
								<el-button text :disabled="!scope.row.hasChildren"  @click="editHandle(scope.row)">
									查看下级
								</el-button>&emsp;
								<el-button text type="primary"  @click="addCategory('edit',scope.row.data)">
									编辑
								</el-button>&emsp;
								<el-button text type="danger" @click="deleteHandle(scope.row.data)">
									删除
								</el-button>
							</template>
						</el-table-column>
					</el-table>
				</section>
			</el-card>
		</div>
		<addCategoryDialog ref="addCategoryDialogRef" @refresh="getCategoryList()" />
	</div>
</template>

<script setup lang="ts">
import { reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { productCategoryList } from "/@/api/commodity";
const addCategoryDialog = defineAsyncComponent(() => import('/@/views/commodity/category/components/addCategoryDialog.vue'));
// 定义变量内容
const addCategoryDialogRef = ref();
const state = reactive({
	param: {
		type: 0,
		name: '',
	},
	storeCategoryList:[] as Array<any>,
	shopCategoryList:[] as Array<any>,
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			search: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});


// 打开新增编辑分类弹窗
const addCategory = (type: string, row ?: Object) => {
	addCategoryDialogRef.value.openDialog(type, state.param.type, JSON.parse(JSON.stringify(row)));
};

const editHandle = (row: any) => {
	if(row.hasChildren){
		ElMessage.success('提交成功');
	}else{
		ElMessage.success('暂无下级');
	}
}

const deleteHandle = (row: any) => {
	ElMessageBox.confirm(
		'确定删除?',
		'提示',
		{
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		}
	)
	.then(() => {
		
    })
    .catch(() => {
       
    })
}


// 获取商品分类  
const getCategoryList = async () => {
	state.tableData.loading = true
	const { results } = await productCategoryList({ platformType: state.param.type });
	state.tableData.data = results
	state.tableData.loading = false
}

// 页面加载时
onMounted(() => {
	getCategoryList();
});

</script>

<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';
:deep(.el-card__body){
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;
	.el-table{
		flex: 1;
		.el-table__inner-wrapper::before{
			background: none !important;
		}
	}
}
</style>

